﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Web;

namespace BaseLib.Data
{
    public static class ContextHelper<TContext> where TContext : ObjectContext, new()
    {
        private const string ObjectContextKey = "ObjectContext";

        public static TContext GetCurrentContext()
        {
            HttpContext httpContext = HttpContext.Current;
            if (httpContext != null)
            {
                string contextTypeKey = ObjectContextKey + typeof(TContext).Name;
                if (httpContext.Items[contextTypeKey] == null)
                {
                    httpContext.Items.Add(contextTypeKey, new TContext());
                }
                return httpContext.Items[contextTypeKey] as TContext;
            }
            throw new ApplicationException("There is no Http Context available");
        }
    }
}
